#include "UsrAI.h"
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;

tagGame tagUsrGame;
ins UsrIns;


double calculateDistance(double x1, double y1, double x2, double y2) {
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

void UsrAI::processData() {
    tagInfo info = getInfo();

    static int currentFrame = info.GameFrame;                                       // 当前帧数

    static int houseCount   = 0;                                                    // 已建造的房子数量
    static int farmerCount  = 0;                                                    // 已生产的村民数量
    static int dockCount    = 0;                                                    // 已建造的船坞数量
    static int sailingCount = 0;                                                    // 已生产的渔船数量
    static int chopCount    = 0;                                                    // 砍树农民数量
    static int huntCount    = 0;                                                    // 打猎农民数量

        // 生产村民
        for (tagBuilding& building : info.buildings) {
            if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD && farmerCount < 8)
            {
                int result = BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);
                if (result > 0) {
                    farmerCount++;
                    DebugText("生产村民成功，当前村民数量: " + std::to_string(farmerCount));
                }
            }
        }

        // 建造房屋
        for (tagFarmer& farmer : info.farmers) {
            if (farmer.NowState == HUMAN_STATE_IDLE && info.Wood >= BUILD_HOUSE_WOOD && houseCount < 2) {
                int x = 10, y = 10; // 假设房屋建造位置
                int result = HumanBuild(farmer.SN, BUILDING_HOME, x, y);
                if (result > 0) {
                    houseCount++;
                    DebugText("建造房屋成功，当前房屋数量: " + std::to_string(houseCount));
                }
            }
        }

        // 建造船坞
        if (dockCount < 1) {
            for (tagFarmer& farmer : info.farmers) {
                if (farmer.NowState == HUMAN_STATE_IDLE && info.Wood >= BUILD_DOCK_WOOD) {
                    int x = 5, y = 5; // 假设船坞建造位置
                    int result = HumanBuild(farmer.SN, BUILDING_DOCK, x, y);
                    if (result > 0) {
                        dockCount++;
                        DebugText("建造船坞成功，当前船坞数量: " + std::to_string(dockCount));
                    }
                }
            }
        }

        // 生产渔船
        for (tagBuilding& building : info.buildings) {
            if (building.Type == BUILDING_DOCK && building.Project == 0 && info.Wood >= BUILDING_DOCK_CREATE_SAILING_WOOD && sailingCount < 1) {
                int result = BuildingAction(building.SN, BUILDING_DOCK_CREATE_SAILING);
                if (result > 0) {
                    sailingCount++;
                    DebugText("生产渔船成功，当前渔船数量: " + std::to_string(sailingCount));
                }
            }
        }

        // 分配农民任务
        for (tagFarmer& farmer : info.farmers) {
            if (farmer.NowState == HUMAN_STATE_IDLE) {
                // 找到最近的资源点
                double minDistance = numeric_limits<double>::max();
                int targetResourceSN = -1;
                int taskType = -1;  // 任务类型：0=砍树，1=打猎

                // 查找最近的树木
                for (tagResource& resource : info.resources) {
                    if (resource.Type == RESOURCE_TREE && resource.Cnt > 0) {
                        double distance = calculateDistance(farmer.DR, farmer.UR, resource.DR, resource.UR);
                        if (distance < minDistance) {
                            minDistance = distance;
                            targetResourceSN = resource.SN;
                            taskType = 0;
                        }
                    }
                }

                // 查找最近的羚羊
                for (tagResource& resource : info.resources) {
                    if (resource.Type == RESOURCE_GAZELLE && resource.Cnt > 0) {
                        double distance = calculateDistance(farmer.DR, farmer.UR, resource.DR, resource.UR);
                        if (distance < minDistance) {
                            minDistance = distance;
                            targetResourceSN = resource.SN;
                            taskType = 1;
                        }
                    }
                }

                // 根据任务类型分配任务
                if (taskType == 0 && chopCount < 4) {  // 限制最多4个农民砍树
                    HumanAction(farmer.SN, targetResourceSN);
                    DebugText("Farmer " + std::to_string(farmer.SN) + " assigned to chop tree.");
                    chopCount++;
                } else if (taskType == 1 && huntCount < 2) {  // 限制最多2个农民打猎
                    HumanAction(farmer.SN, targetResourceSN);
                    DebugText("Farmer " + std::to_string(farmer.SN) + " assigned to hunt gazelle.");
                    huntCount++;
                }
            }
        }
}
